package com.smile.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import com.smile.utils.param.VerificationCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericToStringSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * Created by LiYiChuang
 * on 2017/11/27.
 * 验证码存redis工具类
 */
@Component
public class VerificationCodeTool {
    private static final String  UUID = "verificationCodeId";

    private static final ObjectMapper MAPPER = new ObjectMapper();

    @Autowired
    private  RedisTemplate<String,String> redisTemplate;

    /**
     * 产生唯一自增
     * @return
     */
    public Long getUuid(){
        //redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new GenericToStringSerializer<String>(String.class));
        redisTemplate.opsForValue().setIfAbsent(UUID,"0");
        long id = Long.valueOf(redisTemplate.opsForValue().increment(UUID,1));
        return id;
    }


    /**
     * 去掉标识位
     * @param key
     * @return
     */
    public String getKey(String key){
        return key.substring(1);
    }
    /**
     * 默认验证码过期时间 3分钟
     * @param type type 0 注册 1 修改密码
     * @param mobilephone
     * @param code 验证码
     * @return
     */
    public Long set(int type,String mobilephone,String code) throws JsonProcessingException {
        String id = String.valueOf(getUuid());
        VerificationCode obj = new VerificationCode();
        obj.setCode(code);
        obj.setMobilephone(mobilephone);
        //1.未验证
        obj.setVarifyStatus(1);
        String value = MAPPER.writeValueAsString(obj);
        //默认3分钟
        redisTemplate.opsForValue().set(id,value,3,TimeUnit.MINUTES);
        Long time =  redisTemplate.getExpire(id);
        Long token = Long.valueOf(type+id);
        return token;
    }

    /**
     * 根据key 取
     * @param key
     * @return
     */
    public VerificationCode get(String key) throws IOException {
        //第一位为标识位
        key = getKey(key);
        String verificationCodeStr =  redisTemplate.opsForValue().get(key);
        VerificationCode verificationCode = MAPPER.readValue(verificationCodeStr,VerificationCode.class);
        return verificationCode;
    }

    public  Long getExpire(String key) throws IOException {
        //第一位为标识位
        key = getKey(key);
        Long time =  redisTemplate.getExpire(key);
        return time;
    }
    /**
     * 刷新为30分钟
     * @param key
     * @return true 为成功 false 失败
     */
    public boolean updateDateVerificationCode(String key) throws IOException {
        boolean flag = false;
        VerificationCode verificationCode = get(key);
        if(null != verificationCode) {
            //2.已验证
            verificationCode.setVarifyStatus(2);
            //更新值
            String value = MAPPER.writeValueAsString(verificationCode);
            redisTemplate.opsForValue().set(getKey(key),value);
        }
        //刷新时间
        flag = redisTemplate.expire(getKey(key),30, TimeUnit.MINUTES);

        return flag;
    }

}
